package com.cnyz.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.cnyz.utils.HttpStatus;
import com.cnyz.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

@Data
@ApiModel(description = "返回码")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseResult<T> implements Serializable {

    @ApiModelProperty(value = "状态马")
    private int code;

    @ApiModelProperty(value = "状态信息")
    private String msg;

    @ApiModelProperty(value = "返回结果")
    private T data;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
    private Date operationTime;

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }//无参返回

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        if(StringUtils.isNotNull(data)) {
            this.data = data;
        }
    }//有参返回

    //空参数：默认msg返回
    //msg,返回空data
    //单数据：默认msg构造

    public static ResponseResult success() {
        return ResponseResult.success("操作成功");
    }

    public static ResponseResult success(Object data) {
        return ResponseResult.success("操作成功",data);
    }

    public static ResponseResult success(String msg) {
        return ResponseResult.success(msg,null);
    }

    public static ResponseResult success(String msg,Object data) {
        return new ResponseResult(HttpStatus.SUCCESS,msg,data);
    }

    //erro无data返回,默认null

    public static ResponseResult error() {
        return ResponseResult.error("操作失败");
    }

    public static ResponseResult error(String msg) {
        return ResponseResult.error(msg,null);
    }

    public static ResponseResult error(String msg,Object data) {
        return new ResponseResult(HttpStatus.ERROR,msg,data);
    }


}
